package com.lagou.module01.servlet;

import com.lagou.module01.bean.ClassGrade;
import com.lagou.module01.bean.PageBean;
import com.lagou.module01.service.imp.ClassGradeServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: xyk
 * @Date: Create in 22:45 2021/3/14
 * @Description: 分页展示班级信息
 */
@WebServlet(name = "ClassGradePageQueryServlet", urlPatterns = "/classGradePageQueryServlet")
public class ClassGradePageQueryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取前端传过来的页码
        String page = request.getParameter("page");
        System.out.println("page = "+page);
        PageBean pageBean = null;
        if (page != null){
            pageBean = new PageBean(Integer.parseInt(page), 6);
        }else{
            page = String.valueOf(1);
            pageBean = new PageBean(1, 6);
        }
        // service层获取班级信息
        ClassGradeServiceImp classGradeServiceImp = new ClassGradeServiceImp();
        // 当前页所有班级信息存储在list中
        System.out.println(pageBean);
        List<ClassGrade> classGrades = classGradeServiceImp.showClassGradesService(pageBean);

        //所有班级总数
        int classGradeCount = classGradeServiceImp.countClassGradesService();

        // 计算总页数
        int totalPages = classGradeCount%6 == 0 ? classGradeCount/6:classGradeCount/6 + 1;

        // 将总页数和所有班级列表放入内置对象中，放入session中
        request.getSession().setAttribute("ClassGradesList", classGrades);
        request.getSession().setAttribute("totalPages", totalPages);
        request.getSession().setAttribute("currentPage", page);

        // 重定向到班级管理界面
        response.sendRedirect("classManage.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
